package com.egreen.alipay.service.IAlipayServiceApiImpl;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundAccountQueryModel;
import com.alipay.api.request.AlipayFundAccountQueryRequest;
import com.alipay.api.response.AlipayFundAccountQueryResponse;
import com.egreen.common.serviceapi.alipay.IAlipayServiceApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.egreen.alipay.config.AlipayConfig.getCertAlipayRequest;

/**
 * @PackagePathcom.egreen.alipay.service.IAlipayServiceApiImpl
 * @Classname IAlipayServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/9/21 11:16
 * @Created by Volcan
 */
@Slf4j
@Service
public class IAlipayServiceImpl implements IAlipayServiceApi {

    @Override
    public String queryBusBalance() {
        String availableAmount;
        availableAmount = "-1";
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(getCertAlipayRequest());
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        AlipayFundAccountQueryRequest queryRequest = new AlipayFundAccountQueryRequest();
        AlipayFundAccountQueryModel model = new AlipayFundAccountQueryModel();
        model.setAlipayUserId("2088621987431214");//查询余额所用的商家appid
        model.setAccountType("ACCTRANS_ACCOUNT");
        queryRequest.setBizModel(model);
        AlipayFundAccountQueryResponse response = null;
        try {
            response = alipayClient.certificateExecute(queryRequest);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        String body = response.getBody();
        log.info(body);
        if (!response.isSuccess()){
            log.info("查询商家余额调用AlipaySDK失败");
            return availableAmount;
        }else{
            availableAmount = response.getAvailableAmount();
        }
        return availableAmount;
    }
}
